package com.andrew.demo.controller;
import com.andrew.demo.dao.StudentDao;
import com.andrew.demo.dao.UserDao;
import com.andrew.demo.exception.UserTransactionException;
import com.andrew.demo.model.Girl;
import com.andrew.demo.model.Student;
import com.andrew.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class HomeController {

    @Autowired
    private Girl girl;

    @Autowired
    private Environment environment;

    @Autowired
    private UserDao userdao;

    @Autowired
    private StudentDao studentDao;

    @RequestMapping("/index")
    public String index(){
        return "index"+environment.getProperty("url");
    }

    @RequestMapping("/girl")
    public Girl girl(){
        return girl;
    }

    @RequestMapping(value="/list", method=RequestMethod.GET)
    public List<User> list() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        List<User> list = userdao.getUser();
        return list;
    }

    @RequestMapping(value="/list2", method=RequestMethod.GET)
    public List<Student> list2() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        List<Student> list=studentDao.QueryList();
        return list;
    }

    @RequestMapping(value = "/setAge", method = RequestMethod.GET)
    public String setUserAge(@RequestParam(value="id") int id, @RequestParam(value="addnum") int addnum) {
        try{
            userdao.setAge(id, addnum);
            return "Ok";
        }catch (UserTransactionException e){
            return e.getMessage();
        }

    }

    @RequestMapping(value = "/insert", method = RequestMethod.GET)
    public String insert( User user) {
        try{
            userdao.insert();
            return "Ok";
        }catch (UserTransactionException e){
            return e.getMessage();
        }

    }

    @RequestMapping(value = "/batchUpdate", method = RequestMethod.GET)
    public String batchInsert() {
        try{
            userdao.batchUpdate();
            return "Ok";
        }catch (UserTransactionException e){
            return e.getMessage();
        }

    }


}
